<template>
	<div> 
		<div class="container">
			<TableCustom :columns="columns" :tableData="tableData" :total="page.total" :refresh="getData" 
			:currentPage="page.index"  :changePage="changePage" >
				<template #scopt="{rows,index}">
					<el-button type="primary" @click="handleSession(rows)" size="mini">时间表</el-button>
					<el-button type="danger" @click="handleDelete(rows)" size="mini">取消选课</el-button>
				</template>
			</TableCustom>

		</div>
		<Sessions :visible="sessionVisible" :sessionList="sessionList" :handle-close="()=>{sessionVisible=false}"/>
	</div>
</template>

<script setup lang="ts" name="basetable">
import { ref, reactive } from 'vue';
import { ElMessage, } from 'element-plus';
import { CirclePlusFilled } from '@element-plus/icons-vue';
import { fetchStudentCourse,fetchSessionByTcId ,deleteStudentCourse} from '@/api/index';
import TableCustom from '@/components/table-custom.vue';
import TableDetail from '@/components/table-detail.vue';
import TableSearch from '@/components/table-search.vue';
import Sessions from '@/components/sessions.vue';
import { TableItem } from '@/types/table';
import { FormOption, FormOptionList } from '@/types/form-option';

const sessionList = ref([]);
const sessionVisible = ref(false);
const handleSession = async (row) => {
	let res = await fetchSessionByTcId(row.teachClass.tc_id)
	sessionVisible.value = true;
	sessionList.value = res.data;
};



// 基本信息
const studentCourseInfo = [
	{ prop: "course.name", label: '课程' },
	{ prop: 'teachClass.teacher.name', label: '教师' },
	{ prop: "course.credit", label: '学分'},
	{ prop: 'grade', label: '成绩' },
]
let columns = ref([
	{ type: 'index', label: '序号', width: 55, align: 'center' },
	...studentCourseInfo,
	{ prop: 'scopt', label: '操作', width: 250 }
])


const page = reactive({
	index: 1,
	size: 10,
	total: 200,
})
const changePage = async (val: number) => {
	const sid=localStorage.getItem('sid')
	const res = await fetchStudentCourse({index:val,size:page.size,sid:sid})
	res.data.pageList.forEach(item => {
		item.grade=item.grade>=0?item.grade:'暂无成绩'
	})
	tableData.value = res.data.pageList;
	page.total = res.data.pageNumber;
	page.index = val;
};




const tableData = ref<TableItem[]>([]);
const getData = async () => {
	changePage(1)
};
getData();



// 删除相关
const handleDelete = async (row) => {

	deleteStudentCourse({tc_id:row.teachClass.tc_id}).then((data)=>{
		ElMessage.success(data.data);
		getData();
	}).catch((err)=>{
		ElMessage.error(err.response.data);
	})
}




</script>

<style scoped>

</style>
